home *** CD-ROM | disk | FTP | other *** search
/ HyperLib 1997 Winter - Disc 1 / HYPERLIB-1997-Winter-CD1.ISO.7z / HYPERLIB-1997-Winter-CD1.ISO / オンラインウェア / BUS / TMCM Software and Labs.sit / Software for TMCM 7_95 / Files for Lab 13 / 3N+1 Max next >
Text File  |  1995-07-08  |  1KB  |  44 lines

  1. {
  2.    This program finds the length of the longest 3N+1
  3.    sequence for all starting values of N less than or
  4.    equal to 30.
  5.  
  6.    The program uses multitasking.  Thirty turtles are
  7.    created.  Each one computes a different 3N+1 sequence,
  8.    and these computations all take place simultaneously.
  9. }
  10.   
  11.  
  12. DECLARE max   { The length of the longest sequence }
  13. DECLARE StartValueForMax  { Starting value for that sequence }
  14.  
  15.  
  16. SUB three_n_max(values)
  17.    IMPORT max, StartValueForMax  { The subroutine computes values for }
  18.    max := 0                      {      these two global variables.   }
  19.    fork(values)
  20.    DECLARE n
  21.    DECLARE ct
  22.    n := ForkNumber
  23.    ct := 1
  24.    LOOP
  25.       EXIT IF n = 1
  26.       ct := ct + 1
  27.       IF n/2 = trunc(n/2) THEN
  28.          n := n/2
  29.       ELSE
  30.          n := n*3 + 1
  31.       END IF
  32.    END LOOP
  33.    GRAB max THEN   { Note that only global variables can be used in GRAB statements. }
  34.       IF ct > max THEN
  35.          max := ct
  36.          StartValueForMax := ForkNumber
  37.       END IF
  38.    END GRAB
  39. END SUB
  40.  
  41. three_n_max(30)
  42.  
  43. TellUser("The longest sequence was #max steps long, and it occured for a starting value of #StartValueForMax.")
  44.